<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 应用配置
 */
namespace app\allwin\controller;
use app\allwin\model\AllwinInfoConfig;
use app\allwin\model\AllwinConfig;

class Setting extends Common{

    /**
     *  应用配置
     * @return void
     */
    public function config(){
        if(request()->isAjax()){
            $data = [
                'service_telephone'       => $this->request->param('service_telephone/s'),
                'default_charges'         => $this->request->param('default_charges/d', 0),
                'cash_charges'            => $this->request->param('cash_charges/d', 0),
                'agent_rebate'            => $this->request->param('agent_rebate/d', 0),
                'store_rebate'            => $this->request->param('store_rebate/d', 0),
                'workers_rebate'          => $this->request->param('workers_rebate/d', 0),
                'company_rebate'          => $this->request->param('company_rebate/d', 0),
                'is_psp'                  => $this->request->param('is_psp/d', 0),
                'is_wechat_profitsharing' => $this->request->param('is_wechat_profitsharing/d', 0),
                'is_wechat_touser'        => $this->request->param('is_wechat_touser/d', 0),
                'is_wechat_redpacket'     => $this->request->param('is_wechat_redpacket/d', 0),
                'workers_on_amount'       => $this->request->param('workers_on_amount/f', 0),
                'agent_on_amount'         => $this->request->param('agent_on_amount/f', 0),
                'workers_on_store'        => $this->request->param('workers_on_store/f', 0),
                'workers_discount'        => $this->request->param('workers_discount/f', 0),
                'is_dopay_on_vip'         => $this->request->param('is_dopay_on_vip/d', 0),
                'is_fees_types'           => $this->request->param('is_fees_types/d', 0),
            ];
            $validate = $this->validate($data,'config.save');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result =  AllwinConfig::configs($data,$this->member_miniapp_id);
            if($result){
                return json(['code'=>200,'data' => ['url' => url('setting/config')],'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }else{
            $view['info']     = AllwinConfig::getConfig($this->member_miniapp_id);
            $view['pathMaps'] = [['name' =>' 系统配置','url' => url("setting/config")]];
            return view()->assign($view);

        }
    }

    /**
     *  同城信息配置
     * @return void
     */
    public function info(){
        if(request()->isAjax()){
            $data = [
                'today'  => $this->request->param('today/a'),
                'charge' => $this->request->param('charge/d'),
                'bring'  => $this->request->param('bring/d'),
                'reward' => $this->request->param('reward/d'),
            ];
            $validate = $this->validate($data,'info.config');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            if(empty($data['today'])){
                return json(['code'=>0,'msg'=>'请设置城市号配置']);
            }
            $result =  AllwinInfoConfig::edit($data,$this->member_miniapp_id);
            if($result){
                return json(['code'=>200,'data' => ['url' => url('setting/info')],'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }else{
            $info  = AllwinInfoConfig::Config($this->member_miniapp_id);
            if(empty($info)){
                $view['info'] = [];
            }else{
                $view['info'] = json_decode($info->topday,true); 
            }
            $view['config'] = $info;
            $view['pathMaps'] = [['name' =>'推广设置','url' => url("setting/info")]];
            $view['tabs']    = [
                ['name' =>'主题管理','url' =>url('info.cate/index')],
                ['name' =>'模板库','url' =>url('info.tpl/index')],
                ['name' =>'推广设置','url' =>url('setting/info'),'action' => 1],
            ];
            return view()->assign($view);
        }
    } 

    /**
     * 模板消息配置
     */
    public function tplmsg(){
        if(request()->isAjax()){
            $data = [
                'tplmsg_order' => $this->request->param('tplmsg_order/s'),
                'tplmsg_point' => $this->request->param('tplmsg_point/s'),
            ];
            $result =  AllwinConfig::configs($data,$this->member_miniapp_id);
            if($result){
                return json(['code'=>200,'data' => ['url' => url('setting/tplmsg')],'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }else{
            $view['config'] = AllwinConfig::getConfig($this->member_miniapp_id);
            $view['pathMaps'] = [['name' =>' 模板消息','url' => url("setting/tplmsg")]];
            return view()->assign($view);
        }
    } 

    /**
     * 增加会员制度
     * @return void
     */
    public function sla(){
        if(request()->isAjax()){
            $data['sla'] = $this->request->param('sla/s');
            $result =  AllwinConfig::configs($data,$this->member_miniapp_id);
            if($result){
                return json(['code'=>200,'url' => url('setting/sla'),'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }else{
            $view['config']  = AllwinConfig::where(['member_miniapp_id' => $this->member_miniapp_id])->find();
            $view['pathMaps'] = [['name' =>' 服务协议','url' => url("setting/sla")]];
            return view()->assign($view);
        }
    }

    //文案配置
    public function article(){
        $info =  AllwinConfig::where(['member_miniapp_id' => $this->member_miniapp_id])->find();
        if(request()->isAjax()){
            $data = [
                'shore_img'          => $this->request->param('shore_img/s'),
                'shore_text'         => $this->request->param('shore_text/s'),
                'search_tip'         => $this->request->param('search_tip/s'),
                'index_more'         => $this->request->param('index_more/s'),
                'index_shop_title'   => $this->request->param('index_shop_title/s'),
                'shop_share_text'    => $this->request->param('shop_share_text/s'),
                'shop_share_img'     => $this->request->param('shop_share_img/s'),
                'store_share_text'   => $this->request->param('store_share_text/s'),
                'store_share_img'    => $this->request->param('store_share_img/s'),
                'vip_front_text'     => $this->request->param('vip_front_text/s'),
                'vip_behind_text'    => $this->request->param('vip_behind_text/s'),
                'user_card'          => [
                    'title' => $this->request->param('user_card_title/s'),
                    'page'  => $this->request->param('user_card_page/s'),
                    'type'  => $this->request->param('user_card_type/s')
                ]
            ];
            $validate = $this->validate($data,'config.article');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $config['article'] = json_encode($data);
            $result = AllwinConfig::configs($config,$this->member_miniapp_id);
            if($result){
                return enjson(200,'操作成功',['url' => url('setting/article')]);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
                return enjson(0,'操作失败');
            }
        }else{
            $view['article'] = empty($info->article) ? '' : json_decode($info->article,true);
            $view['pathMaps'] = [['name' =>' 文案配置','url' => url("setting/article")]];
            return view()->assign($view);
        }
    }
}